<?php
$basedir = dirname(__FILE__);
require_once $basedir . '/../classes/minime.php';
MiniMe::initialize();

if( count($argv) != 3) {
      echo "\n\tERROR!";
      echo "\n\t\tPlease provide USERNAME NUMBER-OF-ENTRIES-TO-LEFT as parameters to this function.";
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php entry.php koski 100';
      echo "\n\n";
      exit;
}

$username = $argv[1];
$leave      = $argv[2];

$userLoader = new Minime_User_Loader(Minime::$conf);
$user = $userLoader->getUser($username);

if(!$user) {
    echo "\n\t\tDude, the username you want to delte a blog posts does not exists.";
    echo "\n\n";
    exit;
}

$atomLoader = new Minime_Atom_Loader(MiniMe::$conf);
$feed = $atomLoader->getFeed($username);

$entries = $feed->getEntries();
if( !(count($entries) <= $leave) ) {
    $entries = array_slice($entries, $leave);
    foreach ($entries as $entry) {
        $feed->removeEntry($entry->getId()->getId());    
    }
    $atomStorer = new Minime_Atom_Storer(MiniMe::$conf);
    $atomStorer->storeAtom($username, $feed);
}
echo "\n\n\tDONE!\n";